package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.pojo.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Api(tags = "菜品管理")
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> findDishByPage(DishPageQueryDTO dishPageQueryDTO){
        PageResult dishById = dishService.findDishByPage(dishPageQueryDTO);
        return dishById !=null?Result.success(dishById):Result.error("sb");
    }

    @PostMapping
    @ApiOperation("新增菜品")
    public Result addDish(@RequestBody DishDTO dishDTO){
        int row = dishService.addDish(dishDTO);
        return  row>0 ?Result.success():Result.error("sb");
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询菜品")
    public Result<DishVO> findDishById(@PathVariable Long id){
        DishVO dishById = dishService.findDishById(id);
        return dishById !=null?Result.success(dishById):Result.error("sb");
    }

    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result deleteDish(@RequestParam List<Long> ids){
        int row = dishService.deleteDish(ids);
        return  row>0 ?Result.success():Result.error("sb");
    }
    @PostMapping("/status/{status}")
    @ApiOperation("修改起售禁用状态")
    public Result updateStatus(@PathVariable Integer status,Long id){
        int row = dishService.updateStatus(status, id);
        return  row>0 ?Result.success():Result.error("sb");
    }

    @PutMapping
    @ApiOperation("修改菜品")
    public Result updateDish(@RequestBody DishDTO dishDTO){
        int row = dishService.updateDish(dishDTO);
        return  row>0 ?Result.success():Result.error("sb");
    }

    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> findByCategoryId(Long categoryId){
        List<Dish> byCategoryId = dishService.findByCategoryId(categoryId);
        return byCategoryId != null?Result.success(byCategoryId):Result.error("sb");
    }
}
